package com.sykuku.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sykuku.common.BaseContext;
import com.sykuku.common.R;
import com.sykuku.pojo.AddressBook;
import com.sykuku.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
//地址的 查 增 改默认 查id 改数据
@RestController
@Slf4j
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 查询指定用户的全部地址
     *///http://localhost/front/page/address/list
    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook){
        addressBook.setUserId(BaseContext.getThreadLocalID());
        log.info("addressBook:{}", addressBook);

        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId,addressBook.getUserId());
        List<AddressBook> list = addressBookService.list(lqw);

        return R.success(list);
    }
    /**
     * 用户添加收货的地址
     *///http://localhost/front/page/address

    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook) {

        addressBook.setUserId(BaseContext.getThreadLocalID());
        log.info("addressBook:{}", addressBook);

        addressBookService.save(addressBook);
        return R.success(addressBook);

    }

    /**
     * localhost/addressBook/default
     * @return 设为默认修改地址 1
     */
    @PutMapping("/default")
    public R<AddressBook> defaults(@RequestBody AddressBook addressBook){
        //先把当前用户中所有的地址查出来 批量修改成0
        LambdaUpdateWrapper<AddressBook> luq = new LambdaUpdateWrapper<>();
        luq.eq(AddressBook::getUserId,BaseContext.getThreadLocalID());
        luq.set(AddressBook::getIsDefault,0);

        addressBookService.update(luq);
        //根据地址id单独修改当前用户的默认地址选项
        addressBook.setUserId(BaseContext.getThreadLocalID());
        addressBook.setIsDefault(1);

        addressBookService.updateById(addressBook);

        return R.success(addressBook);
    }

    @GetMapping("/default")
    public R<AddressBook> select(){
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getThreadLocalID());
        queryWrapper.eq(AddressBook::getIsDefault, 1);

        //SQL:select * from address_book where user_id = ? and is_default = 1
        AddressBook addressBook = addressBookService.getOne(queryWrapper);

        if (null == addressBook) {
            return R.error("没有找到该对象");
        } else {
            return R.success(addressBook);
        }
    }

    @GetMapping("/{id}")
    public R<AddressBook> selectById (@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);

        if (addressBook==null){
            R.error("地址不存在哦");
        }

        return R.success(addressBook);
    }


    /**
     *用户修改地址
     * @return
     */

    @PutMapping
    public R<AddressBook> update(@RequestBody AddressBook addressBook){

        addressBook.setUpdateUser(BaseContext.getThreadLocalID());
        addressBook.setUpdateTime(LocalDateTime.now());

        addressBookService.updateById(addressBook);

        return R.success(addressBook);
    }

}
